﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAO;
using DTO;
using BUS;

namespace QLKS
{
    public partial class frmBaoCaoDoanhThu : Form
    {
        public frmBaoCaoDoanhThu()
        {
            InitializeComponent();
        }
        private void frmBaoCaoDoanhThu_Load(object sender, EventArgs e)
        {

        }
        private string catChuoi(string chuoi)
        {
            string c = "";
            string[] a = new string[2];
            a = chuoi.Split('{');
            c = a[1];
            a = c.Split('}');
            c = a[0];
            return c;
        }
        private void ChangeRatioButton(RadioButton rb1, RadioButton rb2)
        {
            if (rb1.Checked == true)
            {
                rb2.Checked = false;
            }
        }
        private void rdbDoanhThuTheoLoaiPhong_CheckedChanged(object sender, EventArgs e)
        {
            ChangeRatioButton(rdbDoanhThuTheoLoaiPhong, rdbDoanhThuTheoMatDoSuDungPhong);
            listView1.Enabled = true;
            listView2.Enabled = false;
            listView2.Items.Clear();
            int m = DateTime.Now.Month;
            cbbThangBaoCao.Items.Clear();
            for (int i = 1; i < m; i++)
            {
                Boolean kq = BCDTTheoLoaiPhongDAO.soSanhThangBaoCaoTheoLoaiPhong(i);
                if (kq == true)
                {
                    cbbThangBaoCao.Items.Add(i);
                }
            }
            label2.Text = "Tổng doanh thu";
        }

        private void rdbDoanhThuTheoMatDoSuDungPhong_CheckedChanged(object sender, EventArgs e)
        {
            ChangeRatioButton(rdbDoanhThuTheoMatDoSuDungPhong, rdbDoanhThuTheoLoaiPhong);
            listView2.Enabled = true;
            listView1.Enabled = false;
            listView1.Items.Clear();
            int m = DateTime.Now.Month;
            cbbThangBaoCao.Items.Clear();
            for (int i = 1; i < m; i++)
            {
                Boolean kq = BCDTTheoMatDoSuDungPhongDAO.soSanhThangBaoCaoTheoMatDo(i);
                if (kq == true)
                {
                    cbbThangBaoCao.Items.Add(i);
                }
            }
            label2.Text = "Tổng số ngày thuê";
        }

        private void btnXemBaoCao_Click(object sender, EventArgs e)
        {
            if (rdbDoanhThuTheoLoaiPhong.Checked == true)
            {
                frmBaoCaoDoanhThuTheoLoaiPhong frm = new frmBaoCaoDoanhThuTheoLoaiPhong();
                frm.ShowDialog();
            }
            if (rdbDoanhThuTheoMatDoSuDungPhong.Checked == true)
            {
                frmBaoCaoDoanhThuTheoMatDoSuDungPhong frm = new frmBaoCaoDoanhThuTheoMatDoSuDungPhong();
                frm.ShowDialog();
            }
        }

        private void btnTinhDoanhThuThang_Click(object sender, EventArgs e)
        {
            if (cbbThangBaoCao.Text == "")
            {
                MessageBox.Show("Bạn chưa chọn tháng báo cáo");
                return;
            }
            int thang = int.Parse(cbbThangBaoCao.SelectedItem.ToString());
            int nam = DateTime.Now.Year;

            if (rdbDoanhThuTheoLoaiPhong.Checked == true)
            {
                DataTable dt = PhieuThuePhongDAO.LayTongDoanhThuTheoThangBaoCao(thang, nam);
                float tongDoanhThu = 0;
                DateTime d = new DateTime();
                foreach (DataRow dr in dt.Rows)
                {
                    float thanhTien = float.Parse(dr["ThanhTien"].ToString());
                    tongDoanhThu = tongDoanhThu + thanhTien;
                    d = DateTime.Parse(dr["NgayKetThuc"].ToString());
                }
                DataTable dt2 = BCDTTheoLoaiPhongDAO.GetAll();
                BCDTTheoLoaiPhongDTO doanhThu = new BCDTTheoLoaiPhongDTO();
                doanhThu.ThangBaoCao =d;
                doanhThu.TongDoanhThu = tongDoanhThu;
                BCDTTheoLoaiPhongDAO.InsertBCDTTheoLoaiPhong(doanhThu);
                txtTongDoanhThu.Text = tongDoanhThu.ToString();
                DataTable dt1 = PhieuThuePhongDAO.LayPhieuThueTheoThangBaoCao(thang, nam);
                int i = 1;
                listView1.Items.Clear();
                foreach (DataRow dr1 in dt1.Rows)
                {
                    ListViewItem lv1 = new ListViewItem(i.ToString());
                    for (int j = 0; j < dt1.Columns.Count; j++)
                    {
                        lv1.SubItems.Add(dr1[j].ToString());
                    }
                    float tt = float.Parse(dr1["ThanhTien"].ToString());
                    float tyLe = (tt * 100) / tongDoanhThu;
                    double TyLe = Math.Round(double.Parse(tyLe.ToString()), 1);
                    lv1.SubItems.Add(TyLe.ToString());
                    int flag = 0;
                    for (int k = 0; k < listView1.Items.Count; k++)
                    {
                        String a = listView1.Items[k].SubItems[1].ToString();
                        String b = lv1.SubItems[1].ToString();
                        if (a == b)
                        {
                            float tT = float.Parse(catChuoi(listView1.Items[k].SubItems[2].ToString()));
                            float tL = float.Parse(catChuoi(listView1.Items[k].SubItems[3].ToString()));
                            tT = tT + float.Parse(catChuoi(lv1.SubItems[2].ToString()));
                            tL = tL + float.Parse(catChuoi(lv1.SubItems[3].ToString()));
                            double TL = Math.Round(double.Parse(tL.ToString()), 1);
                            listView1.Items[k].SubItems.RemoveAt(3);
                            listView1.Items[k].SubItems.RemoveAt(2);
                            listView1.Items[k].SubItems.Add(tT.ToString());
                            listView1.Items[k].SubItems.Add(TL.ToString());
                            flag = 1;
                            break;
                        }
                    }
                    if (flag == 0)
                    {
                        listView1.Items.Add(lv1);
                    }
                    i++;
                }
                DataTable dt3 = CT_BCDTTheoLoaiPhongDAO.GetAll();
                DataTable dt5 = BCDTTheoLoaiPhongDAO.LayMaxMaBCDTTheoLoaiPhong();
                for (int l = 0; l < listView1.Items.Count; l++)
                {
                    String tenLoaiPhong = catChuoi(listView1.Items[l].SubItems[1].ToString());
                    DataTable dt4 = LoaiPhongDAO.LoaiPhongTheoTen(tenLoaiPhong);
                    DataRow dr4 = dt4.Rows[0];
                    int maLoaiPhong = int.Parse(dr4["MaLoaiPhong"].ToString());
                    String doanhthu = catChuoi(listView1.Items[l].SubItems[2].ToString());
                    String tyle = catChuoi(listView1.Items[l].SubItems[3].ToString());
                    CT_BCDTTheoLoaiPhongDTO ctdt = new CT_BCDTTheoLoaiPhongDTO();
                    ctdt.MaBaoCaoDoanhThu = int.Parse(dt5.Rows[0].ItemArray[0].ToString());
                    ctdt.MaLoaiPhong = maLoaiPhong;
                    ctdt.DoanhThu = float.Parse(doanhthu);
                    ctdt.TyLe = float.Parse(tyle);
                    CT_BCDTTheoLoaiPhongDAO.InserCT_BCDTTheoLoaiPhong(ctdt);
                }

            }
            if (rdbDoanhThuTheoMatDoSuDungPhong.Checked == true)
            {
                DataTable dt = PhieuThuePhongDAO.LayTongSoNgayThueTheoThangBaoCao(thang, nam);
                int tongNgayThue = 0;
                DateTime d = new DateTime();
                foreach (DataRow dr in dt.Rows)
                {
                    int soNgayThue = int.Parse(dr["SoNgayThue"].ToString());
                    tongNgayThue = tongNgayThue + soNgayThue;
                    d = DateTime.Parse(dr["NgayKetThuc"].ToString());
                }

                DataTable dt2 = BCDTTheoMatDoSuDungPhongDAO.GetAll();
                BCDTTheoMatDoSuDungPhongDTO matDo = new BCDTTheoMatDoSuDungPhongDTO();
                matDo.ThangBaoCao = d;
                matDo.TongNgayThue = tongNgayThue;
                BCDTTheoMatDoSuDungPhongDAO.InsertBCDTTheoMatDoSuDungPhong(matDo);
                txtTongDoanhThu.Text = tongNgayThue.ToString();
                DataTable dt1 = PhieuThuePhongDAO.LayPhieuThueMatDoTheoThangBaoCao(thang, nam);
                int aaa = dt1.Rows.Count;
                int i = 1;
                listView2.Items.Clear();
                foreach (DataRow dr1 in dt1.Rows)
                {
                    ListViewItem lv1 = new ListViewItem(i.ToString());
                    for (int j = 0; j < dt1.Columns.Count; j++)
                    {
                        lv1.SubItems.Add(dr1[j].ToString());
                    }
                    int snt = int.Parse(dr1["SoNgayThue"].ToString());
                    float tyLe = (snt * 100) / tongNgayThue;
                    double TyLe = Math.Round(double.Parse(tyLe.ToString()), 1);
                    lv1.SubItems.Add(TyLe.ToString());
                    int flag = 0;
                    for (int k = 0; k < listView2.Items.Count; k++)
                    {
                        String a = listView2.Items[k].SubItems[1].ToString();
                        String b = lv1.SubItems[1].ToString();
                        if (a == b)
                        {
                            int snT = int.Parse(catChuoi(listView2.Items[k].SubItems[2].ToString()));
                            float tL = float.Parse(catChuoi(listView2.Items[k].SubItems[3].ToString()));
                            snT = snT + int.Parse(catChuoi(lv1.SubItems[2].ToString()));
                            tL = tL + float.Parse(catChuoi(lv1.SubItems[3].ToString()));
                            double TL = Math.Round(double.Parse(tL.ToString()), 1);
                            listView2.Items[k].SubItems.RemoveAt(3);
                            listView2.Items[k].SubItems.RemoveAt(2);
                            listView2.Items[k].SubItems.Add(snT.ToString());
                            listView2.Items[k].SubItems.Add(TL.ToString());
                            flag = 1;
                            break;
                        }
                    }
                    if (flag == 0)
                    {
                        listView2.Items.Add(lv1);
                    }
                    i++;
                }
                DataTable dt3 = CT_BCDTTheoMatDoSuDungPhongDAO.GetAll();
                DataTable dt5 = BCDTTheoMatDoSuDungPhongDAO.LayMaxMaBCDTTheoMatDoSuDungPhong();
                for (int l = 0; l < listView2.Items.Count; l++)
                {
                    String tenPhong = catChuoi(listView2.Items[l].SubItems[1].ToString());
                    DataTable dt4 = PhongDAO.PhongTheoTen(tenPhong);
                    DataRow dr4 = dt4.Rows[0];
                    int maPhong = int.Parse(dr4["MaPhong"].ToString());
                    String songaythue = catChuoi(listView2.Items[l].SubItems[2].ToString());
                    String tyle = catChuoi(listView2.Items[l].SubItems[3].ToString());
                    CT_BCDTTheoMatDoSuDungPhongDTO ctmd = new CT_BCDTTheoMatDoSuDungPhongDTO();
                    ctmd.MaBCMD = int.Parse(dt5.Rows[0].ItemArray[0].ToString());
                    ctmd.MaPhong = maPhong;
                    ctmd.SoNgayThue = int.Parse(songaythue);
                    ctmd.TyLe = float.Parse(tyle);
                    CT_BCDTTheoMatDoSuDungPhongDAO.InserCT_BCDTTheoMatDoSuDungPhong(ctmd);
                }
            }

            int m = DateTime.Now.Month;
            cbbThangBaoCao.Items.Clear();
            for (int i = 1; i < m; i++)
            {
                Boolean kq = BCDTTheoLoaiPhongDAO.soSanhThangBaoCaoTheoLoaiPhong(i);
                if (kq == true)
                {
                    cbbThangBaoCao.Items.Add(i);
                }
            }
            cbbThangBaoCao.Text = "";
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

       
    }
}
